<?php
namespace app\erp\model;

use think\Model;

class Visitdb extends Model
{

    protected $pk = 'visit_id';

    protected $name = 'visit';

    protected $type = [
        'create_time' => 'timestamp:Y-m-d H:i'
    ];
    // 自动完成时间转换
    protected $autoWriteTimestamp = true;

    public $param = array();

    public function scopeMemberId($query, $member_id)
    {
        $query->where('member_id', $member_id);
    }

    public function scopeGoodsSku($query, $goods_sku)
    {
        $query->where('goods_sku', $goods_sku);
    }
    // 基础查询
    public function base($query)
    {
        if (isset($this->param['goods_sku']) && $this->param['goods_sku']) {
            $query->goodsSku($this->param['goods_sku']);
        }
        if (isset($this->param['member_id']) && $this->param['member_id']) {
            $query->memberId($this->param['member_id']);
        }
    }

    public function ajaxFind()
    {
        $data = $this->find();
        if ($data) {
            return $data->toArray();
        } else {
            return array();
        }
    }
    public function getVisitGoodsSkus(){
        $data = $this->order('update_time desc')->limit($this->param['limit'])->select();
        if($data){
            $data = $data->toArray();
            $goods_id_arr = array();
            foreach ($data as $key=>$val){
                array_push($goods_id_arr, $val['goods_sku']);
            }
        } else {
            $goods_id_arr = -1;
        }
        return $goods_id_arr;
    }
    public function ajaxSave()
    {
        $chk = $this->ajaxFind();
        if ($chk) {
            $this->isUpdate(true)->save($this->param, array(
                'visit_id' => $chk['visit_id']
            ));
        } else {
            $this->isUpdate(false)->save($this->param);
        }
        return 'success';
    }
}